package com.takeaway.service.serviceimpl;

import com.takeaway.const_.Const;
import com.takeaway.dao.daointerface.FoodDao;
import com.takeaway.dao.daolmpl.FoodDaoImpl;
import com.takeaway.pojo.Book;
import com.takeaway.pojo.Cart;
import com.takeaway.pojo.Food;
import com.takeaway.service.serviceinterface.FoodService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 后台赖骏
 */
public class FoodServiceImpl implements FoodService {
    private static final FoodDao FOOD_DAO = new FoodDaoImpl();
    @Override
    public List<Food> showLimitFood(int begin, int listNum, int shopId) throws Exception {
        return FOOD_DAO.selectListMore(Food.class,begin,listNum,shopId);
    }
    @Override
    public List<Food> showShopFood(int shopId) throws Exception {
        String[] checkField = {Const.FIELD_SHOP_ID};
        return FOOD_DAO.selectMore(Food.class,checkField,shopId);
    }

    @Override
    public long checkFoodNum(int shopId) throws Exception {
        return FOOD_DAO.selectListNum(shopId);
    }
    @Override
    public List<Food> checkCartFood(List<Cart> carts) throws Exception{
        ArrayList<Food> foods = new ArrayList<>();
        for (Cart cart:carts) {
            String[] checkField2 ={Const.FIELD_ID};
            Food food = FOOD_DAO.selectSingle(new Food(), checkField2, cart.getFoodId());
            foods.add(food);
        }
        return foods;
    }

    @Override
    public List<Food> checkBookFood(List<Book> books) {
        return null;
    }

    @Override
    public int addFood(String disName, String description, double price,int shopId) throws Exception {

        return FOOD_DAO.insert(new Food(disName,price,description,shopId));
    }

    @Override
    public int deleteFood(int foodId) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        return FOOD_DAO.delete(Food.class,checkField,foodId);
    }

    @Override
    public int updateFood(int foodId, String disName, String description, double price) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        Food food = FOOD_DAO.selectSingle(new Food(), checkField, foodId);
        food.setDishName(disName);
        food.setPrice(price);
        food.setDescription(description);
        return FOOD_DAO.update(food);
    }

    @Override
    public int updatePhoto(int foodId,String photoName) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        Food food = FOOD_DAO.selectSingle(new Food(), checkField, foodId);
        food.setPhoto(photoName);
        return FOOD_DAO.update(food);
    }

    @Override
    public Food checkSingleFood(int foodId) throws Exception {
        String[] checkField = {Const.FIELD_ID};
        return FOOD_DAO.selectSingle(new Food(),checkField,foodId);
    }
}
